package com.xjy.spring.thrift.zk;

import com.xjy.spring.thrift.config.BaseServerConfig;
import org.I0Itec.zkclient.ZkClient;

/**
 * thrift服务注册
 *
 * @author xujieyang
 */
public class ThriftServiceRegister {
    private ZkClient zkClient;

    public ThriftServiceRegister(String connectString, int sessionTimeout, int connectTimeout) {
        zkClient = new ZkClient(connectString, sessionTimeout, connectTimeout);
    }

    // TODO 并发性能优化 CAS
    public synchronized void register(BaseServerConfig config, String rootPath, String thriftServicePath) {
        if (!zkClient.exists(rootPath)) {
            zkClient.createPersistent(rootPath);
        }
        zkClient.createEphemeralSequential(thriftServicePath, config);
    }
}
